<?php
/**
 * @name Front处理dispatch
 *
 * LICENSE
 *
 * This source file is subject to the BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.phpxe.com/license/
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to Chaodong.Sheng <scd113@gmail.com> so we can send you a copy immediately.
 *
 * @category   Front of Scd Framework
 * @package    Scd
 * @copyright  Copyright (c) 2007 PHPXe Web Studio <http://www.phpxe.com>
 * @license    http://framework.phpxe.com/license/
 * @version    $Id: Front.php Tue Sep 25 22:30:21 CST 2007 Davy $
 */
include_once("Scd/Controller/Exception.php");
include_once("Scd/Controller/Action.php");
include_once("Scd/Controller/Dispatch.php");

class Scd_Controller_Front{
	/**
	 * 配置文件句柄
	 */
	var $_config = null;

	/**
	 * sonfig中设置controller_path命名空间，但暂时不使用
	 * 避免麻烦。
	 */
	var $_controllerDir = null;
	
	var $_applicationDir = null;
	
	var $_instance = null;

	/**
	 * getInstance()方法用来获取前端控制器实例。
	 * 实现单件模式，
	 *
	 */
	function getInstance()
	{
		return new Scd_Controller_Front();
	}

	/**
	 * 得到config信息
	 * @param 来自config的instance
	 */
	function getConfig( $instance )
	{
		if (null === $this->_config) {
			$this->_config = $instance;
		}
		return $this->_config;
	}

	/**
	 * Dispatch
	 *
	 * @param 配置文件句柄（sonfig） $instance
	 */
	function dispatch()
	{
		$this->_config->app_path = $this->_applicationDir;
		new Scd_Controller_Dispatch( $this->_config );
	}

	/**
     * Convenience feature, calls setControllerDirectory()->setRouter()->dispatch()
     *
     * may actually be called after setting up your front controller.
     *
     * @param string|array $controllerDirectory Path to Zend_Controller_Action
     * controller classes or array of such paths
     * @return void
     */
	function run($configIns)
	{
		$ins = Scd_Controller_Front::getInstance();//获得自己的操纵句柄
		$ins->getConfig($configIns);
		$ins->setControllerDirectory($configIns->app_path);//设置默认的controllerDir
		$ins->dispatch();//开始分配
	}

	/**
     * 设置应用文件夹;这个是预留出来为因特殊情况需要设置特别的application目录的
     * 比如调试新项目
     */
	function setApplicationDirectory($directory)
	{
		$this->_applicationDir = $directory;
		return $this;
	}

	/**
     * 设置 controller 的文件夹
     *
     * Stores controller directory to pass to dispatcher. May be an array of
     * directories or a string containing a single directory.
     *
	 */
	function setControllerDirectory($directory, $module = null)
	{
		$this->setApplicationDirectory($directory);
		if (empty($module) || is_numeric($module) || !is_string($module)) {
			//$module = $this->getDispatcher()->getDefaultModule();
			$module = $this->_defaultModule;
		}

		$this->_controllerDir[$module] = rtrim((string) $this->_applicationDir .DIRECTORY_SEPARATOR. $this->_defaultModule, '/\\');
		return $this;
	}
}